#include <QCoreApplication>

#include "QHttpClient.h"

using namespace QtNet;

// 子结构体
struct Address
{
    QString city;
    QString street;
    int zipCode;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Address,city,street,zipCode)

// 父结构体（包含嵌套）
struct Person
{
    QString name;
    int age;
    Address address;  // 嵌套结构体
    QDateTime time;
    QVector<QString> hobbies;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Person,name,age,address,time,hobbies)

struct result1
{
    int status;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(result1,status)

struct result2
{
    int status;
    QString mesaage;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(result2,status,mesaage)

// 发送Person对象
void test_post_person_object()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setUrl(QUrl("http://192.168.2.122:8081/json"));

    Person p1, p2, p3;
    p1.age = 1;
    p1.time = QDateTime::currentDateTime();

    QHttpClient::post(request, p1, [](result1 v, QNetworkReply *reply) {

    });

    p2.age = 10;
    p3.age = 100;

    QList<Person> list;
    list << p1 << p2 << p3;

    QHttpClient::post(request, list, [](result2 v, QNetworkReply *reply) {

    });
}

// 发送QVector<int>
void test_post_vector_int()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setUrl(QUrl("http://192.168.2.122:8081/json"));

    QVector<int> v;
    v << 1 << 2 << 3;
    QHttpClient::post(request, v, [](result1 v, QNetworkReply *reply) {

    });

    QVector<QVector<int>> vv;
    vv << v << v;
    QHttpClient::post(request, vv, [](result2 v, QNetworkReply *reply) {

    });
}

// 发送复杂QMap结构
void test_post_complex_map()
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setUrl(QUrl("http://192.168.2.122:8081/json"));

    QMap<QString, QVector<int>> m;

    QVector<int> v1;
    v1 << 1 << 2 << 3;
    m["v1"] = v1;

    QVector<int> v2;
    v2 << 3 << 2 << 1;
    m["v2"] = v2;

    QHttpClient::post(request, m, [](result2 v, QNetworkReply *reply) {

    });
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    test_post_person_object();
    test_post_vector_int();
    test_post_complex_map();

    return a.exec();
}
